<?php
class Server {
  public static function upload($orm, $sys, $inst, $is_iframe) {
    if (isset($_FILES['Filedata']) && is_uploaded_file($_FILES['Filedata']['tmp_name']) && $_FILES['Filedata']['error'] == 0) {
      //获得上传文件信息
      $upload_file = $_FILES['Filedata'];
      //获取文件扩展名
      $file_info = pathinfo($upload_file['name']);
      $file_ext = (isset($file_info['extension'])) ? $file_info['extension'] : '';
      
      $updir = UPLOAD_DIR_PLGN . $inst->__folder;
      if (!file_exists($updir)) {
        Utils::create($updir);
      }
        
      //获取客户端参数
      $item = Utils::get_param('item', '');
      $item_n = $item . '_n';
      $data = $orm->get('pb_colg_template', NULL, NULL, NULL, NULL, NULL)->d;
      if (count($data) == 0) {
        $obj = new stdClass();
        $obj->$item = '';
        $obj->$item_n = '';
        $orm->add('pb_colg_template', $obj);
        $obj->id = $orm->last_id();
        $data = array($obj);
      }

      if (file_exists($data[0]->$item) && is_file($data[0]->$item)) {
        unlink($data[0]->$item);
      }
      
      //Add custom code here...
      $file_ext = strtolower((isset($file_info['extension'])) ? $file_info['extension'] : '');
      $res = new stdClass();
      $res->id = $data[0]->id;
      $res->$item = $updir . '/' . Utils::unique() . '.' . $file_ext;
      $res->$item_n = $_FILES['Filedata']['name'];
      if (move_uploaded_file($upload_file['tmp_name'], $res->$item)) {
        $orm->update('pb_colg_template', $res);
        echo json_encode($res);
      }
    }
  }
  
  public static function download($orm, $sys, $inst, $is_iframe) {
  }
  
  public static function remove($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('req', '');
    $req_n = $req . '_n';
    
    $data = $orm->get('pb_colg_template', NULL, NULL, NULL, NULL, NULL)->d;
    if (count($data) > 0) {
      if (file_exists($data[0]->$req) && is_file($data[0]->$req)) {
        unlink($data[0]->$req);
      }
      $data[0]->$req = '';
      $data[0]->$req_n = '';
      $orm->update('pb_colg_template', $data[0]);
    }
  }
}
?>